/*
 * Created by SharpDevelop.
 * User: Michael
 * Date: 13/09/2007
 * Time: 10:58 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;

namespace SudokuSolver
{
	/// <summary>
	/// Slice and Dice is a passive technique which eliminates values from cells.
	/// It finds cells that contain values, and then eliminates that value from all the
	/// other cells in the same column, row and block.
	/// </summary>
	public class SliceAndDice : Technique
	{
		
		private bool SliceValues(Group theCells)
		{
			//Console.WriteLine("Starting Slice and Dice");
			bool changed = false;
			//if any cell is set, eliminate it from the other cells
			foreach(Cell thisCell in theCells.Cells)
			{
				if(thisCell.Found)
				{
					foreach(Cell otherCell in theCells.Cells)
					{
						//except the one we're looking at
						if(!(thisCell == otherCell))
						{
							if(otherCell.IsPossible(thisCell.Value))
							{
								//if it was, it's impossible to put that value here
								otherCell.IsPossible(thisCell.Value, false);
								//something has changed!
								changed = true;
							}
						}
					}
				}
			}
			
			return changed;
		}
		
		public override bool Run(SudokuGame theGame)
		{
			bool changed = false;
			foreach(Group rows in theGame.Rows)
			{
				changed = (changed || SliceValues(rows));
			}
			foreach(Group columns in theGame.Columns)
			{
				changed = (changed || SliceValues(columns));
			}
			foreach(Group blocks in theGame.Blocks)
			{
				changed = (changed || SliceValues(blocks));
			}
			
			return changed;
		}
		
		public override string ToString()
		{
			return base.ToString() + ": Slice and Dice";
		}
	}
}
